package GUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JPanel;

import logic.flowfree;
import logic.pos;

import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class GamePanel extends JPanel {
	
	 
	private static final long serialVersionUID = 1L;
	public Color[] cores = {Color.white,Color.red,Color.blue,Color.green,Color.yellow,Color.orange,
							Color.GRAY,Color.cyan,Color.magenta,Color.darkGray, Color.LIGHT_GRAY,
							Color.PINK, new Color(159,95,159), new Color(92,64,51),new Color(47,79,47),new Color(235,199,158)};
	public flowfree fl;
	public Cell[][] boardCells;
	static public GamePanel self;
	public GamePanel(flowfree fl) {
		
		self = this;
		this.fl=fl;
		boardCells = new Cell[fl.matrix.length][fl.matrix.length];
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0};
		gridBagLayout.rowHeights = new int[]{0};
		gridBagLayout.columnWeights = new double[]{Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE};
	
		setLayout(gridBagLayout);
		
		for(int i =0; i< fl.matrix.length;i++)
		{
			for(int j = 0 ; j< fl.matrix.length;j++){

				Cell cell = new Cell(i,j,fl.matrix[i][j],cores[fl.matrix[i][j]%100]);
			 
				 
				GridBagConstraints gbc_lblCell = new GridBagConstraints();
				gbc_lblCell.insets = new Insets(0, 0, 0, 0);
				gbc_lblCell.gridx = j;
				gbc_lblCell.gridy = i;
				gbc_lblCell.ipady = 20;
				gbc_lblCell.ipadx = 20;
				
			 
				add(cell, gbc_lblCell);
				boardCells[i][j] = cell;
			}
		}
		((JPanel)GUI.frame.getContentPane()).revalidate();
		GUI.frame.repaint();
		
	}
	public boolean canPlay(pos initPos, pos finalPos)
	{
		if(!initPos.adjacent(finalPos))
			return false;
		else
		{
			int valIni=boardCells[initPos.getX()][initPos.getY()].getValue();
			int valFinal=boardCells[finalPos.getX()][finalPos.getY()].getValue();
			if(valFinal ==0 ||(valIni>100 && valFinal>100 && valIni%100 == valFinal%100) )
				return true;
			else return false;
		}
		
	}
	public void cleanColor(Color c)
	{
		boolean initFound = false;
		for(int i =0; i< boardCells.length;i++)
		{
			for(int j = 0 ; j< boardCells.length;j++){
				
				if(boardCells[i][j].getColor()== c)
					if(boardCells[i][j].isEndPoint() && !initFound)
					{
						initFound = true;
						int val = (boardCells[i][j].getValue()%100)+100;
						boardCells[i][j].setValue(val);
						GUI.fl.matrix[i][j]=val;
					}
					else if(boardCells[i][j].isEndPoint() && initFound)
					{
						int val = (boardCells[i][j].getValue()%100)+200;
						boardCells[i][j].setValue(val);
						GUI.fl.matrix[i][j]=val;
					}
					else{
						boardCells[i][j].setValue(0);
						boardCells[i][j].setColor(Color.WHITE);
						GUI.fl.matrix[i][j]=0;
					}
			}
		}
	}
	public boolean intersect(pos position,Color c)
	{
		Cell cell = boardCells[position.getX()][position.getY()];
		if(cell.getValue()<100 && !cell.getColor().equals(c) )
			return true;
		else return false;
	}
	public boolean  reversePlay(pos initPos, pos finalPos,pos previous)
	{
		if(!initPos.adjacent(finalPos))
			return false;
		else
		{
			if(previous==null)
				return true;
			if(!finalPos.equals(previous) )
				return false;
			int valIni=boardCells[initPos.getX()][initPos.getY()].getValue();
			int valFinal=boardCells[finalPos.getX()][finalPos.getY()].getValue();
			if(valFinal<100 && valIni>100 && valIni%100 == valFinal%100) 
				return true;
			else return false;
		}
	}
	

}
